using DevExpress.Blazor;
using EasyXaf.LowCode.WorkflowEditors.Models.Activities;
using EasyXaf.LowCode.WorkflowEditors.UndoManagers;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

namespace EasyXaf.LowCode.WorkflowEditors.Nodes;

public partial class AnnotationActivityComponent : IAsyncDisposable
{
    private ElementReference AnnotationEleReference { get; set; }

    private IJSObjectReference JsModule { get; set; }

    private DxMemo AnnotationMemo { get; set; }

    private string AnnotationValue { get; set; }

    private bool AnnotationEditing { get; set; }

    private string ComponentStyle
    {
        get => string.Join(";", new List<string>
        {
            $"--wf-node-border-color:{Annotation.Color}",
            $"--wf-node-backcolor:{Annotation.Color}10",
            $"--wf-node-scale:{Annotation.TitleViewScale}"
        });
    }

    private bool IsReadOnly
    {
        get => WorkflowMode >= WorkflowMode.Debug;
    }

    [CascadingParameter]
    public IUndoManager UndoManager { get; set; }

    [CascadingParameter]
    public WorkflowMode WorkflowMode { get; set; }

    [Parameter]
    public IActivity Annotation { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> ShowAnnotationMenu { get; set; }

    [Inject]
    public IJSRuntime JSRuntime { get; set; }

    private void ProcessKeyDown(KeyboardEventArgs e)
    {
        if (e.Code == "Escape")
        {
            if (AnnotationEditing)
            {
                CancelEditAnnotation();
            }
        }
    }

    public void BeginEditAnnotation()
    {
        if (!IsReadOnly)
        {
            AnnotationValue = Annotation.Comment;
            AnnotationEditing = true;
        }
    }

    private void CancelEditAnnotation()
    {
        AnnotationEditing = false;
    }

    private async Task EndEditAnnotationAsync()
    {
        if (Annotation.Comment != AnnotationValue)
        {
            UndoManager.ChangeNodeComment(Annotation, AnnotationValue);
        }
        AnnotationEditing = false;
        await JsModule.InvokeVoidAsync("setAnnotation", AnnotationEleReference, Annotation.Comment);
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            var jsModuleFile = "./_content/EasyXaf.LowCode.WorkflowEditors/Nodes/AnnotationActivityComponent.razor.js";
            JsModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import", jsModuleFile);
            await JsModule.InvokeVoidAsync("setAnnotation", AnnotationEleReference, Annotation.Comment);
        }
        if (AnnotationEditing && AnnotationMemo != null)
        {
            await AnnotationMemo.FocusAsync();
        }
    }

    public async ValueTask DisposeAsync()
    {
        try
        {
            if (JsModule != null)
            {
                await JsModule.DisposeAsync();
                JsModule = null;
            }
        }
        catch (JSDisconnectedException)
        {
        }

        GC.SuppressFinalize(this);
    }
}